package com.sample.springbatch.job.sample.config;

import com.sample.springbatch.job.config.BaseConfig;
import com.sample.springbatch.job.listener.MyJobListener;
import com.sample.springbatch.job.listener.MyStepListener;
import com.sample.springbatch.job.sample.processor.UserItemProcessor;
import com.sample.springbatch.job.sample.service.TaskletStepService;
import com.sample.springbatch.model.User;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.step.tasklet.MethodInvokingTaskletAdapter;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.task.SyncTaskExecutor;

import javax.sql.DataSource;


/**
 * Created by zwg on 2017/7/14.
 */
@Configuration
public class JobListenerConfig extends BaseConfig {

    @Autowired
    MyJobListener myJobListener;
    @Autowired
    MyStepListener myStepListener;

    @Autowired
    TaskletStepService taskletStepService;


    @Bean(name = "jobListenerJob")
    public Job taskLetJob() {
        return jobBuilderFactory.get("jobListenerJob")
                .incrementer(new RunIdIncrementer())
                .listener(myJobListener)
                .start(jobListenerStep())
                .build();
    }

    @Bean
    public Step jobListenerStep() {
        return stepBuilderFactory.get("jobListenerStep").listener(myStepListener)
                .tasklet(taskletExecOk())
                //.tasklet(taskletExecError())
                //.taskExecutor(new SyncTaskExecutor())
                //.throttleLimit(5)
                .build();
    }



    @Bean
    public Tasklet taskletExecOk(){
        MethodInvokingTaskletAdapter tasklet = new MethodInvokingTaskletAdapter();
        tasklet.setTargetObject(taskletStepService);
        tasklet.setTargetMethod("execOk");
        return tasklet;
    }

    @Bean
    public Tasklet taskletExecError(){
        MethodInvokingTaskletAdapter tasklet = new MethodInvokingTaskletAdapter();
        tasklet.setTargetObject(taskletStepService);
        tasklet.setTargetMethod("execError");
        return tasklet;
    }

}
